/*
 * @ModuleCode:
 * @Author: 张泽涛
 * @Date: 2023-08-07 16:08:02
 * @LastEditors: 张泽涛
 * @LastEditTime: 2024-01-05 10:24:20
 * @Description:
 */
define([ELMP.resource('flow/DoubleLinkedList.js', 'lib'), ELMP.resource('index.js', 'workflow_sdk'), 'EventBus'], function (
  DoubleLinkedList, WorkflowSdk, EventBus
) {
  'use strict'
  function init () {
    const flowMap = {
    }
    return {
      // 获取流程字典
      flowMap,
      // 根据流程id打开流程审批
      async openFlowApprovalByProcessId (processInstanceId, callback) {
        const processDefinitionIdData = await $.el.get({
          url: ELMP.url('/workflow/v1/procinst/query/{0}', processInstanceId)
        })
        const processDefinitionId = processDefinitionIdData.res.data.processDefinitionId
        const taskIdList = await $.elApi.flow.runningusertask([processInstanceId])
        const runningUserTaskList = taskIdList[processInstanceId]
        if (runningUserTaskList && runningUserTaskList.length) {
          window.CACHE_.taskId = (_.filter(runningUserTaskList, function (item) {
            return item.assignee && item.assignee.indexOf($.el.auth.user.id) !== -1
          })[0] || {}).id
        } else {
          window.CACHE_.taskId = undefined
        }
        const res = await $.el.get({
          url: ELMP.url('/workflow/v1/task/taskview'),
          data: {
            processInstanceId,
            processDefinitionId,
            taskId: window.CACHE_.taskId
          }
        })
        const pboId = res.res.processInstanceDto.businessKey
        WorkflowSdk.openProcessWorkshop({
          pboId,
          processInstanceId,
          processDefinitionId,
          taskId: window.CACHE_.taskId,
          closeCallback () {
            window.CACHE_.source = '' // 清空
            window.CACHE_.processDefinitionKey = ''
            callback && callback()
          }
        })
      },
      // 根据单据id打开流程审批
      async openFlowApproval (id, callback) {
        const resp = await $.el.get({
          url: ELMP.url('/workflow/v1/business/process/ids/{0}', id)
        })
        if (resp.res.data) {
          const { processInstanceId, processDefinitionId, runningUserTaskList } = resp.res.data
          if (runningUserTaskList && runningUserTaskList.length) {
            window.CACHE_.taskId = (_.filter(runningUserTaskList, function (item) {
              return item.assignee && item.assignee.indexOf($.el.auth.user.id) !== -1
            })[0] || {}).id
          } else {
            window.CACHE_.taskId = undefined
          }
          const res = await $.el.get({
            url: ELMP.url('/workflow/v1/task/taskview'),
            data: {
              processInstanceId,
              processDefinitionId,
              taskId: window.CACHE_.taskId
            }
          })
          const pboId = res.res.processInstanceDto.businessKey
          WorkflowSdk.openProcessWorkshop({
            pboId,
            processInstanceId,
            processDefinitionId,
            taskId: window.CACHE_.taskId,
            closeCallback () {
              window.CACHE_.source = '' // 清空
              window.CACHE_.processDefinitionKey = ''
              callback && callback()
            }
          })
        }
      },
      // 创建流程
      createFlowMap (flowList = []) {
        const link = new DoubleLinkedList()
        flowList.forEach(flow => {
          link.append(flow)
        })
        return link
      },
      createWorkflowRegisterConfig ({ category, businessKey, stepUrl }) {
        EventBus.emit('workflow:register:config:ExecutionLayout', {
          [`${category}-${businessKey}`]: function (callback) {
            callback(stepUrl)
          }
        })
      }
    }
  }
  return {
    init
  }
})
